package com.hnichr.controller;

import com.hnichr.pojo.Plate;
import com.hnichr.pojo.Result;
import com.hnichr.service.StopService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/stop/")
public class StopController {


    @Autowired
    private StopService stopService;

    @GetMapping("snapshot")
    public Result<String> snapshot(){
        String imagePath = stopService.snapshot();
        return Result.OK(imagePath);
    }

    @GetMapping("open")
    public Result<String> open(){
        String result = stopService.open();
        return Result.OK(result);
    }

    @GetMapping("whitelist/query/{plate}")
    public Result<List<Plate>> whitelistQuery(@PathVariable(name = "plate", required = true) String plate){
        List<Plate> plateList = stopService.writeListQuery(plate);
        return Result.OK(plateList);
    }

    @GetMapping("whitelist/addOrUpdate")
    public Result<String> whitelistAddOrUpdate(String plate,String context,String enableTime,String overdueTime){
        String result = stopService.writeListAddOrUpdate(plate,context,enableTime,overdueTime,1);
        return Result.OK(result);
    }

    @GetMapping("whitelist/delete/{plate}")
    public Result<String> whitelistDel(@PathVariable(name = "plate", required = true) String plate){
        String result = stopService.writeListDel(plate);
        return Result.OK(result);
    }

    @GetMapping("whitelist/enableOrDisable/{plate}")
    public Result<String> whitelistEnableOrDisable(@PathVariable(name = "plate", required = true) String plate){
        List<Plate> plateList = stopService.writeListQuery(plate);
        if(CollectionUtils.isEmpty(plateList)&&CollectionUtils.size(plateList)>1){
            return Result.error("车牌不合法");
        }
        Plate plateVo = plateList.get(0);
        String result = stopService.writeListAddOrUpdate(plate,plateVo.getContext(),plateVo.getEnableTime(),plateVo.getOverdueTime(),plateVo.getEnable()==1?0:1);
        return Result.OK(result);
    }

    /*@GetMapping("iostatus/{type}/{gpio}")
    public Result<String> whitelistDel(@PathVariable(name = "type", required = true) String type,@PathVariable(name = "gpio", required = true) String gpio){
        String result = stopService.ioStatus(type,gpio);
        return Result.OK(result);
    }*/
}
